package cn.wolfcode.shop.controller;

import cn.wolfcode.shop.domain.*;
import cn.wolfcode.shop.service.*;
import cn.wolfcode.shop.vo.JSONResult;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品属性相关
 */
@Controller
@RequestMapping("skuPropertys")
public class SkuPropertyController {
    @Reference
    private ICatalogService catalogService;
    @Reference
    private ISkuPropertyService propertyService;
    @Reference
    private ISkuPropertyValuesService propertyValuesService;
    @RequestMapping("view")
    public String view(Model model){
        //查询所有商品分类
        List<Catalog> catalogs = catalogService.listAll();
        String allCatalog = JSON.toJSONString(catalogs);
        model.addAttribute("allCatalog",allCatalog);
        return "sku_property/property";
    }

    @PostMapping
    @ResponseBody
    public JSONResult saveOrUpdate(SkuProperty property){
        JSONResult jsonResult=new JSONResult();
        propertyService.saveOrUpdate(property);
        return jsonResult;
    }

    @DeleteMapping("{id}")
    @ResponseBody
    public JSONResult deleteById(@PathVariable("id") Long id){
        JSONResult jsonResult=new JSONResult();
        propertyService.deleteById(id);
        return jsonResult;
    }

    /**
     * 获取指定属性的属性值信息
     * @return 返回模板
     */
    @GetMapping("{propertyId}/propertyValues")
    public String getPropertyValues(Model model,@PathVariable("propertyId") Long propertyId){
        List<SkuPropertyValue> propertyValues=propertyValuesService.getValuesByPropertyId(propertyId);
        model.addAttribute("propertyValueList",propertyValues);
        return "property/property_value_list";
    }

    /**
     * 获取指定的sku属性对应的属性值列表
     * @param model
     * @param skuPropertyId
     * @return 返回一个列表视图
     */
    @GetMapping("{skuPropertyId}/skuPropertyValues/table")
    public String listSkuPropertyVal(Model model,@PathVariable("skuPropertyId") Long skuPropertyId){
        //获取sku属性
        SkuProperty skuProperty=propertyService.getById(skuPropertyId);
        //获取sku属性下的属性值列表
        List<SkuPropertyValue> skuPropertyValueList = propertyValuesService.getValuesByPropertyId(skuPropertyId);
        model.addAttribute("skuPropertyValueList",skuPropertyValueList);
        model.addAttribute("skuProperty",skuProperty);
        return "product_sku/sku_property_value_table";
    }

}
